package kr.pe.xissy.soulmate.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.RequestException;
import com.google.gwt.http.client.Response;
import com.google.gwt.http.client.URL;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.RootPanel;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class SoulMate implements EntryPoint {
	private Button clickMeButton;

	public void onModuleLoad() {
		RootPanel rootPanel = RootPanel.get("basicContainer");

		clickMeButton = new Button();
		rootPanel.add(clickMeButton);
		clickMeButton.setText("Click me!");
		clickMeButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				Window.alert(Window.Location.getPath());

				String url = "../";
				url = URL.encode(url);

				RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, url);

				try {
					Request request = builder.sendRequest(null, new RequestCallback() {
						public void onError(Request request, Throwable exception) {
							// displayError("Couldn't retrieve JSON");
							Window.alert("Couldn't retrieve JSON");
						}

						public void onResponseReceived(Request request, Response response) {
							Window.alert(String.valueOf(response.getStatusCode()));

							if (200 == response.getStatusCode()) {
								// updateTable(asArrayOfStockData(response.getText()));
								Window.alert(response.getText());
							} else {
								// displayError("Couldn't retrieve JSON (" +
								// response.getStatusText()
								// + ")");
								Window.alert("Couldn't retrieve JSON (" + response.getStatusText() + ")");
							}
						}
					});
				} catch (RequestException e) {
					// displayError("Couldn't retrieve JSON");
					Window.alert("Couldn't retrieve JSON");
				}
			}
		});
	}
}
